from typing import *

from sortedcontainers import SortedDict


class Solution:

    def longestEqualSubarray(self, nums: List[int], k: int) -> int:
        n = len(nums)
        ans = l = r = 0
        totNums = SortedDict()
        cnt = Counter()
        while r < n:
            nr = nums[r]
            cnt[nr] += 1
            if cnt[nr] in totNums:
                totNums[cnt[nr]] += 1
            else:
                totNums[cnt[nr]] = 1
            while r - l + 1 - k > totNums.keys()[-1]:
                nl = nums[l]
                totNums[cnt[nl]] -= 1
                if totNums[cnt[nl]] == 0:
                    totNums.pop(cnt[nl])
                cnt[nl] -= 1
                if cnt[nl] in totNums:
                    totNums[cnt[nl]] += 1
                else:
                    totNums[cnt[nl]] = 1
                l += 1
            ans = max(ans, r - l + 1)
            r += 1
        return ans


sd = SortedDict()
sd[10] = 10
sd[2] = 100
print(sd)
print()